
#pragma once

#include <deque>

namespace DuiLib
{
	class CommonTreeNode
	{
	public:
		typedef std::vector<CommonTreeNode*> NodeList;
		typedef std::vector<CVar> CVarVector;

	public:

		CommonTreeNode(int dataColumn);
		~CommonTreeNode();

		CVar data(int index) const;
		bool findData(int index) const;
		void setData(int index, const CVar& data);

		CommonTreeNode* parent() const;
		void setParent(CommonTreeNode* pNode);

		void addChild(CommonTreeNode* pNode);
		void removeChild(CommonTreeNode* pNode);

		NodeList& children();

	private:
		CVarVector _data;

		CommonTreeNode* _parent;
		NodeList _children;
	};


	class UILIB_API CCommonTreeModel : public CAbstractItemModel
	{
		typedef std::deque<CVar> VarLine;
	public:
		CCommonTreeModel() = delete;
		CCommonTreeModel(int rows, int cols);
		~CCommonTreeModel();

		bool hasIndex(int row, int col, const CModelIndex& parent = CModelIndex()) const;
		CModelIndex index(int row, int col, const CModelIndex& parent = CModelIndex()) const;
		CModelIndex sibling(int row, int col, const CModelIndex& idx) const;

		CModelIndex parent(const CModelIndex& child) const;
		bool hasChild(const CModelIndex& parent = CModelIndex()) const;

		int rowCount(const CModelIndex& parent = CModelIndex()) const;
		int columnCount(const CModelIndex& parent = CModelIndex()) const;

		CVar data(const CModelIndex& index) const;
		bool setData(const CModelIndex& index, const CVar& var);

		CVar headerData(int section, Orientation orient) const;
		bool setHeaderData(int section, Orientation orient, const CVar& var);

		bool insertRows(int row, int count, const CModelIndex& parent = CModelIndex());
		bool removeRows(int row, int count, const CModelIndex& parent = CModelIndex());

		bool removeAll();

		CModelIndex getIndexByInterPtr(const void* interPtr);

	private:
		void initHeadData();
		CommonTreeNode* getTreeNode(const CModelIndex& index) const;

	private:
		VarLine m_HeadData;

		CommonTreeNode* m_pRootNode;

		int m_ColumnCount;
	};

}